﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TopArtFinalProject.Models;

namespace TopArtFinalProject.Controllers
{
    public class SongController : Controller
    {
        //
        // GET: /Song/
        SongContext db = new SongContext();
        public ActionResult Index()
        {
            try
            {
                var songs = db.Songs.ToList();

                return View(songs);
            }
            catch (Exception e)
            {
                return View(e.ToString());
            }
        }

        //
        // GET: /Song/Details/5

        public ActionResult Details(int id)

        {
            Song song = db.Songs.Find(id);
            return View(song);
        }

        //
        // GET: /Song/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Song/Create

        [HttpPost]
        public ActionResult Create(Song song)
        {

            try
            {
                // TODO: Add insert logic here
                db.Songs.Add(song);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Song/Edit/5

        public ActionResult Edit(int id)
        {
            Song song = db.Songs.Find(id);
            return View(song);
        }

        //
        // POST: /Song/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Song song)
        {
            try
            {
                // TODO: Add update logic here
                db.Entry(song).State = EntityState.Modified;
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Song/Delete/5

        public ActionResult Delete(int? id)
        {

            Song song = db.Songs.Find(id);
            if (song == null)
            {
                return HttpNotFound();
            }
            db.Songs.Remove(song);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // POST: /Song/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            Song song = db.Songs.Find(id);
            if(song==null){
                return HttpNotFound();
            }
            db.Songs.Remove(song);
            db.SaveChanges();
            return RedirectToAction("Index");
        }


        //Searching Data//
        public ActionResult Search(string search, string searchType)
        {
            if (searchType==null)
            {
                var songs1 = db.Songs.Where(s => s.Title.Contains(search)).ToList();
                var songs2 = db.Songs.Where(s => s.Album.Contains(search)).ToList();
                var songs3 = db.Songs.Where(s => s.Artist.Contains(search)).ToList();
                var songs4 = db.Songs.Where(s => s.UserName.Contains(search)).ToList();
                var songs5 = db.Songs.Where(s => s.Genre.Contains(search)).ToList();
               // var songs6 = db.Songs.Where(s => (s.ReleseDate.Date.Year).Equals(Convert.ToInt32(search))).ToList();
                var data = songs1.Union(songs2).ToList();
                data = data.Union(songs3).ToList();
                data = data.Union(songs4).ToList();
                data = data.Union(songs5).ToList();
             //   data = data.Union(songs6).ToList();
                return View(data);
            }
            else if (searchType.Equals("Title"))
            {
                
                var songs1 = db.Songs.Where(s => s.Title.Contains(search)).ToList();
                return View(songs1);
            }
            else if (searchType.Equals("Album"))
            {
                var songs2 = db.Songs.Where(s => s.Album.Contains(search)).ToList();
                return View(songs2);
            }

            else if (searchType.Equals("Artist"))
            {
                var songs3 = db.Songs.Where(s => s.Artist.Contains(search)).ToList();
                return View(songs3);
            }

            else if (searchType.Equals("UserName"))
            {
                var songs4 = db.Songs.Where(s => s.UserName.Contains(search)).ToList();
                return View(songs4);
            }
            else if (searchType.Equals("Genre"))
            {
                var songs5 = db.Songs.Where(s => s.Genre.Contains(search)).ToList();
                return View(songs5);
            }

            else if (searchType.Equals("Year"))
            {
                var songs6 = db.Songs.Where(s => s.ReleseDate.Date.Year.ToString().Equals(search));
                return View(songs6);
            }

            else return View();
        }


    }
}
